package cn.dnui_zll731.dnuifood_731zlll.controller;
import android.content.Intent;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import cn.dnui_zll731.dnuifood_731zlll.R;
import cn.dnui_zll731.dnuifood_731zlll.bean.UserBean;
import cn.dnui_zll731.dnuifood_731zlll.listener.RetrofitListener;
import cn.dnui_zll731.dnuifood_731zlll.model.UserModel;

public class RegisterActivity extends BaseActivity implements RetrofitListener<UserBean> {

    private EditText username,userpass,reuserpass,tel,address,comment;
    private Button register,login;
    @Override
    protected int initlayout() {
        return R.layout.activity_register;
    }
    @Override
    void initViews() {
        username=findViewById(R.id.editText3);
        userpass=findViewById(R.id.editText4);
        reuserpass=findViewById(R.id.editText9);
        tel=findViewById(R.id.editText5);
        address=findViewById(R.id.editText6);
        comment=findViewById(R.id.editText7);

        register=findViewById(R.id.button3);
        login=findViewById(R.id.button4);
    }

    @Override
    void initEvents() {

        //注册并返回
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (userpass.getText().toString().equals(reuserpass.getText().toString())){
                    register();//注册
                }else {
                    showToast("请确认两次密码相同");
                }
            }
        });
        //返回到登录页面
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(RegisterActivity.this,LoginActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    void initData() {

    }



    @Override
    public void onSuccess(UserBean object, int flag) {
        if (object.getSuccess().equals("1")){
            //注册成功返回到登录界面、、并且传递账号密码
            Intent intent = new Intent();
            intent.putExtra("username",username.getText().toString());
            intent.putExtra("userpass",userpass.getText().toString());
            setResult(RESULT_OK,intent);
            finish();
            showToast("注册成功");
            finish();
        }else{
            showToast("注册失败，可能用户名重名");
        }
    }

    @Override
    public void onFalure() {//注册信息错误返回消息
        showToast("网络解析错误");
    }

    private void register(){
        UserModel userModel=new UserModel();
        userModel.register(username.getText().toString(),
                userpass.getText().toString(),
                tel.getText().toString(),
                address.getText().toString(),
                comment.getText().toString(),this);

    }
////    Bundle bundle = this.getIntent().getExtras();//二·获取Inter中的数据
////    String result = bundle.getString("data");
}

